package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.activity.client.ActivityFeignClient;
import com.atguigu.gmall.common.result.Result;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author Sanmier
 * @create 2020-12-23 21:02
 */
@Controller
public class SeckillController {

    @Autowired
    private ActivityFeignClient activityFeignClient;


    //秒杀列表
    @GetMapping("seckill.html")
    public String seckillItem(Model model){
        Result result = activityFeignClient.findAll();
        model.addAttribute("list", result.getData());
        return "seckill/index";
    }


    //页面详情
    @GetMapping("seckill/{skuId}.html")
    public String skillGoodsItem(@PathVariable Long skuId,Model model){
        //通过skuId查询skuInfo
        Result result = activityFeignClient.getSeckillGoods(skuId);
        model.addAttribute("item", result.getData());
        return "seckill/item";
    }


    //进入秒杀页面
    @GetMapping("seckill/queue.html")
    public String queue(@RequestParam Long skuId,
                        @RequestParam String skuIdStr,
                        HttpServletRequest request){
        request.setAttribute("skuId", skuId);
        request.setAttribute("skuIdStr", skuIdStr);
        return "seckill/queue";
    }


    //下单页面
    @GetMapping("seckill/trade.html")
    public String seckillTrade(Model model){
        //  页面需要userAddressList， detailArrayList，totalNum，totalAmount 这个四个数据都存在map中。然后使用model 保存


        Result<Map<String,Object>> result = activityFeignClient.trade();
        //        //  保存数据
        //        model.addAllAttributes(result.getData());
        //        //  返回下单页面
        //        return "seckill/trade";
        //  表示返回的200
        if (result.isOk()){
            //保存数据
            model.addAllAttributes(result.getData());
            //返回下单页面
            return "seckill/trade";
        }else {
            //保存数据
            model.addAttribute("message", "下单失败");

            //返回下单页面
            return "seckill/fail";
        }

    }
}
